/*
 * Copyright 2007-2008 Andrew O'Malley
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package ws.quokka.plugin.devreport;

import ws.quokka.core.bootstrap_util.IOUtils;
import ws.quokka.core.plugin_spi.support.AbstractPlugin;
import ws.quokka.core.util.xml.Document;
import ws.quokka.core.util.xml.Element;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;


/**
 *
 */
public class DevReportsPlugin extends AbstractPlugin {
    //~ Methods --------------------------------------------------------------------------------------------------------

    public void xmlSummary() throws IOException {
        DevReportHelper helper = new DevReportHelper(getResources(), getProperties(), logger());
        getUtils().mkdir(helper.getReportsRoot());

        Writer writer = new BufferedWriter(new FileWriter(helper.getReportsFile()));
        toXML(helper, helper.getState().getReports(), writer);
        logger().info("Written reports summary to " + helper.getReportsFile());
    }

    protected Writer toXML(DevReportHelper helper, List reports, Writer writer) {
        Document document = Document.create();
        Element reportsEl = document.addRootElement("reports");
        Collections.sort(reports);

        for (Iterator i = reports.iterator(); i.hasNext();) {
            Report report = (Report)i.next();
            Element reportEl = reportsEl.addChild("report");
            reportEl.addChild("name").addText(report.getName());
            reportEl.addChild("description").addText(report.getDescription());
            reportEl.addChild("index").addText(report.getIndexFile().getAbsolutePath());

            // Calculate the relative URL for the index from the reports root directory
            String relativePath = helper.getRelativePath(report, helper.getReportsRoot());
            reportEl.addChild("relativeindex").addText(relativePath);
        }

        return document.toXML(writer, true);
    }

    public void reports() throws IOException {
        if (logger().isDebugEnabled()) {
            logger().debug("Project: " + getResources().getProject().getName() + ": "
                + System.identityHashCode(getResources().getProject()));
        }

        // Copy index file
        File destDir = getProperties().getFile("destDir");
        File indexFile = new File(destDir, "index.html");
        new IOUtils().copyStream(getResources().getLocalResources().getAsStream("index.html"),
            new FileOutputStream(indexFile));

        // Generate home.html
        Document document = Document.parse(getResources().getLocalResources().getAsStream("home.html"));
        Element tableEl = document.getRoot().getChild("table");
        DevReportHelper helper = new DevReportHelper(getResources(), getProperties(), logger());

        for (Iterator i = helper.getState().getReports().iterator(); i.hasNext();) {
            Report report = (Report)i.next();
            Element trEl = tableEl.addChild("tr");
            trEl.addChild("td").addChild("a").setAttribute("class", "name").setAttribute("target", "ReportFrame")
            .setAttribute("href", helper.getRelativePath(report, destDir)).addText(report.getName());
            trEl.addChild("td").setAttribute("class", "description").addText(report.getDescription());
        }

        document.toXML(new FileWriter(new File(destDir, "home.html")), true);

        // Generate menu.html
        document = Document.parse(getResources().getLocalResources().getAsStream("menu.html"));

        Element divEl = document.getRoot().getChild("div");
        divEl.addChild("a").setAttribute("target", "ReportFrame").setAttribute("href", "home.html").addText("Home");

        for (Iterator i = helper.getState().getReports().iterator(); i.hasNext();) {
            Report report = (Report)i.next();
            divEl.addChild("a").setAttribute("target", "ReportFrame")
            .setAttribute("href", helper.getRelativePath(report, destDir)).addText(report.getName());
        }

        document.toXML(new FileWriter(new File(destDir, "menu.html")), true);
        logger().info("Generated report to: " + indexFile.getAbsolutePath());
    }
}
